package com.keensense.vcm.internal;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.keensense.vcm.HttpMethod;
import com.keensense.vcm.common.auth.CredentialsProvider;
import com.keensense.vcm.common.comm.RequestMessage;
import com.keensense.vcm.common.comm.ServiceClient;
import com.keensense.vcm.common.parser.ResponseParsers;
import com.keensense.vcm.model.GetIntelligentAnalysisListRequest;
import com.keensense.vcm.model.GetIntelligentAnalysisListResponse;
import com.keensense.vcm.model.StartIntelligentAnalysisRequest;
import com.keensense.vcm.model.VCMResponse;

/**
 * 智能分析任务管理
 *
 * @author lanjian
 * @date 2020/5/7 11:05 上午
 */
public class IntelligentAnalysisTaskOperation extends VCMOperation {

    public IntelligentAnalysisTaskOperation(ServiceClient client, CredentialsProvider credsProvider) {
        super(client, credsProvider);
    }

    /**
     * 查询智能分析任务列表
     */
    public GetIntelligentAnalysisListResponse
        getIntelligentAnalysisList(GetIntelligentAnalysisListRequest getIntelligentAnalysisListRequest) {

        String xml = getXmlFromObject(getIntelligentAnalysisListRequest, GetIntelligentAnalysisListRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
            .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.GET_INTELLIGENT_ANALYSIS_LIST);
        GetIntelligentAnalysisListResponse result =
            doOperation(request, ResponseParsers.getIntelligentAnalysisListResponseParser, true);
        return result;
    }

    /**
     * 创建智能分析任务v1.1
     *
     * @param startIntelligentAnalysisRequest
     * @return
     */
    public VCMResponse startIntelligentAnalysis(StartIntelligentAnalysisRequest startIntelligentAnalysisRequest) {
        String xml = getXmlFromObject(startIntelligentAnalysisRequest, StartIntelligentAnalysisRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
            .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.START_INTELLIGENT_ANALYSIS);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * 指定任务编码删除智能分析任务
     *
     * @param taskId
     * @return
     */
    public VCMResponse deleteIntelligentAnalysis(String taskId) {
        Map<String, String> parameters = new HashMap<>(1);
        parameters.put("taskId", taskId);
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setEndpoint(endpoint)
            .setParameters(parameters).setMethod(HttpMethod.GET).build();
        request.setResourcePath(VCMResourcePathConstants.DELETE_INTELLIGENT_ANALYSIS);
        return doOperation(request, emptyResponseParser, true);
    }

}
